package com.yiren.dbaa.module.device.presenter;

import android.app.Activity;
import android.media.AudioManager;

import com.blankj.utilcode.util.BrightnessUtils;
import com.blankj.utilcode.util.VolumeUtils;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.device.bean.DeviceBaseInfoBean;
import com.yiren.dbaa.module.device.bean.DeviceDisplayInfoBean;
import com.yiren.dbaa.module.device.bean.DeviceGjdInfoBean;
import com.yiren.dbaa.module.device.contract.DeviceInfoContract;
import com.yiren.dbaa.module.device.model.DeviceInfoModel;
import com.yiren.dbaa.util.DecimalTransferUtil;
import com.yiren.dbaa.util.DeviceUtil;
import com.yiren.dbaa.util.DistanceUtil;
import com.yiren.dbaa.util.NetWorkUtil;

/**
 * @author lwb
 * @date : 2022/7/6 15:17
 * @description: 设备信息
 */
public class DeviceInfoPresenter implements DeviceInfoContract.Presenter {

    private Activity activity;
    private DeviceInfoContract.Model deviceInfoModel;
    private UserParamConfigBean userParamConfig;

    public DeviceInfoPresenter(Activity activity) {
        this.activity = activity;
        deviceInfoModel = new DeviceInfoModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void getBaseInfo(DeviceInfoContract.View view) {
        DeviceBaseInfoBean deviceBaseInfo = new DeviceBaseInfoBean();
        if(userParamConfig.getUserInfo()!=null){
            deviceBaseInfo.setPhoneNumber((userParamConfig.getUserInfo().getPhone() == null) ? "未登录" : userParamConfig.getUserInfo().getPhone());
        }else {
            deviceBaseInfo.setPhoneNumber("未登录");
        }
        deviceBaseInfo.setAppVersion(DeviceUtil.getAppVersion(activity));
        deviceBaseInfo.setCurSite(null == userParamConfig.getSite() ? "未下载考场数据" : userParamConfig.getSite().getSiteName());
        deviceBaseInfo.setConectDevice((G.connectDeviceMessage == null) ? false : true);
        if (deviceBaseInfo.isConectDevice()) {
            deviceBaseInfo.setHardWareVersion(G.connectDeviceMessage.getVersionDescribe());
            deviceBaseInfo.setDeviceNumber(LoadConfigApi.loadStationParamConfig(activity).getHostNumber());
            deviceBaseInfo.setDeviceCode(G.connectDeviceMessage.getEquipmentCode());
            if (G.connectDeviceMessage.getVersionDescribe().contains("MP")) {
                deviceBaseInfo.setCarModel(new LowerComputerPresenter(activity).getObdCarModelName());
            } else {
                deviceBaseInfo.setDeviceCode("接线板");
            }
        } else {
            deviceBaseInfo.setCarModel("未连接");
            deviceBaseInfo.setHardWareVersion("未连接");
            deviceBaseInfo.setDeviceNumber("未连接");
            deviceBaseInfo.setDeviceCode("未连接");
        }
        view.showBaseInfo(deviceBaseInfo);
    }

    @Override
    public void getDisPlayInfo(DeviceInfoContract.View view) {
        String netWork = (NetWorkUtil.isNetworkAvailable(activity)) ? "已连接" : "未连接";
        deviceInfoModel.getSimCard(new CommonCallBack() {
            @Override
            public void success(Object result) {
                DeviceDisplayInfoBean displayInfoBean = (DeviceDisplayInfoBean) result;
                displayInfoBean.setDeviceConnect((G.connectDeviceMessage == null) ? "未连接" : "已连接");
                displayInfoBean.setNetWork(netWork);
                displayInfoBean.setScreenLight(BrightnessUtils.getBrightness() + "");
                displayInfoBean.setVoiceValue(VolumeUtils.getVolume(AudioManager.STREAM_MUSIC) + "");
                view.showDisplayInfo(displayInfoBean);
            }

            @Override
            public void fail(String error) {
                DeviceDisplayInfoBean displayInfoBean = new DeviceDisplayInfoBean();
                displayInfoBean.setDeviceConnect((G.connectDeviceMessage == null) ? "未连接" : "已连接");
                displayInfoBean.setNetWork(netWork);
                displayInfoBean.setScreenLight(BrightnessUtils.getBrightness() + "");
                displayInfoBean.setVoiceValue(VolumeUtils.getVolume(AudioManager.STREAM_MUSIC) + "");
                displayInfoBean.setTotalNetFlow("");
                displayInfoBean.setRemainNetFlow("");
                displayInfoBean.setActivateTime("");
                if (NetWorkUtil.isNetworkAvailable(activity)){
                    displayInfoBean.setSimType("Wifi");
                }else {
                    displayInfoBean.setSimType("无网络");
                }
                displayInfoBean.setActivateTime("无");
                displayInfoBean.setIccid("N/A");
                view.showDisplayInfo(displayInfoBean);
            }
        });
    }

    @Override
    public void getGjdInfo(DeviceInfoContract.View view) {
        DeviceGjdInfoBean deviceGjdInfo = new DeviceGjdInfoBean();
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
        boolean isShow = userParamConfigBean.isHasGjdInterface();
        //高精度盒子硬件版本
        if (G.connectDeviceMessage != null) {
            deviceGjdInfo.setHardwareVersion("");
        }else {
            deviceGjdInfo.setHardwareVersion("");
        }
        deviceGjdInfo.setBaseStationDistance(DecimalTransferUtil.readableMeter(DistanceUtil.getBaseDistance(GpsInfoBean.getInstance())) + "  KM");
        String stationId = LoadConfigApi.loadStationParamConfig(activity).getBaseStationId();
        String stationName = LoadConfigApi.loadStationParamConfig(activity).getBaseStationName();
        String str = stationId + stationName;
        deviceGjdInfo.setBaseStationName(str);
        //网络识别号
        String netRecogData = LoadConfigApi.loadStationParamConfig(activity).getRoverRegdt();
        deviceGjdInfo.setNetRecogData(netRecogData);


        //高精车模
        String carModelId = LoadConfigApi.loadSignalSourceConfig(activity).getCarModelId();
        if (StringUtil.isEmpty(carModelId)) {
            deviceGjdInfo.setCarModel("未选择");
        } else {
            CarModelEntity carModel = (CarModelEntity) UserDbController.getInstance(false).queryById(carModelId, CarModelEntity.class);
            if (carModel == null) {
                deviceGjdInfo.setCarModel("请先下载车模");
            } else {
                deviceGjdInfo.setCarModel(carModel.getName());
            }
        }

        view.showGjdInfo(deviceGjdInfo, isShow);
    }

    @Override
    public void getCarState(DeviceInfoContract.View view) {
        deviceInfoModel.refreshCarstate(new CommonCallBack() {
            @Override
            public void success(Object result) {
                view.showCarState();

            }
        });
    }

    @Override
    public void removeAllCallBack() {
        deviceInfoModel.removeAllCallBack();
    }



}
